/*
 *所有关于Supplier类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*SupplierService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using VOL.Core.BaseProvider;
using VOL.Core.Extensions.AutofacManager;
using VOL.Entity.DomainModels;
using System.Linq;
using VOL.Core.Utilities;
using System.Linq.Expressions;
using VOL.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using MyMES.IRepositories;
using MyMes.Infrastructure;
using MyMES.Dto;
using System;
using MyMES.Infrastructure;

namespace MyMES.Services
{
    public partial class SupplierService
    {
        private readonly IBaseRepository<Supplier> _supplierRepository;

        public SupplierService(IBaseRepository<Supplier> supplierRepository)
        {
            _supplierRepository = supplierRepository;
        }

        /// <summary>
        /// 获取供应商信息
        /// </summary>
        /// <returns></returns>
        public async Task<APIPage<Supplier>> GetSupplierList(GetSupplierPageListDTO request)
        {
            var list = _supplierRepository.GetAll();

            if (!string.IsNullOrEmpty(request.SupplierCode))
            {
                list = list.Where(e => e.SupplierCode.Contains(request.SupplierCode));
            }
            if (!string.IsNullOrEmpty(request.SupplierName))
            {
                list = list.Where(e => e.SupplierName.Contains(request.SupplierName));
            }
            if (request.SupplierRating != 0)
            {
                list = list.Where(e => e.SupplierRating == request.SupplierRating);
            }
            if (!string.IsNullOrEmpty(request.SupplierReferred))
            {
                list = list.Where(e => e.SupplierReferred.Contains(request.SupplierReferred));
            }

            var supplierpaging = new APIPage<Supplier>()
            {
                Data = list.Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList(),
                TotalCount = list.Count(),
                TotalPage = (int)Math.Ceiling(list.Count() * 1.0 / request.PageSize)
            };

            return supplierpaging;
        }

        /// <summary>
        /// 新增供货商信息
        /// </summary>
        /// <param name="param">供货商参数</param>
        /// <returns></returns>
        public async Task<int> CreateSupplier(Supplier param)
        {
            var result = await _supplierRepository.AddAsync(param);

            return result;
        }

        /// <summary>
        /// 删除供货商信息
        /// </summary>
        /// <param name="param">供货商参数</param>
        /// <returns></returns>
        public async Task<int> DeleteSupplier(Supplier param)
        {
            var result = await _supplierRepository.Remove(param);

            return result;
        }

        /// <summary>
        /// 修改供货商信息
        /// </summary>
        /// <param name="param">供货商参数</param>
        /// <returns></returns>
        public async Task<int> UpdateSupplier(Supplier param)
        {
            var result = await _supplierRepository.UpdateAsync(param);

            return result;
        }

    }
}
